package controller

import (
	"log"
	"testhttp/dao"
	"testhttp/model"
	"testhttp/service"

	"github.com/kataras/iris/v12"
)

func ListRules(ctx iris.Context) {
	page, _ := ctx.URLParamInt("page")
	limit, _ := ctx.URLParamInt("limit")

	ctx.JSON(service.ListRule(page, limit))

}

func ViewRules(ctx iris.Context) {
	uid := ctx.URLParam("ruleuid")
	dealerid := ctx.URLParam("dealerid")
	if uid != "" {
		ctx.JSON(dao.SelectRulebyId(uid))
	}
	if dealerid != "" {
		ctx.JSON(dao.SelectRulebyDealerid(dealerid))
	}

}

func DelRules(ctx iris.Context) {
	// uid := ctx.URLParam("ruleuid")
	var maprule []model.MapRules
	err := ctx.ReadJSON(&maprule)
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	}
	log.Print("Debug requer:", maprule)
	dao.DeleteRules(maprule)

}

func AddRules(ctx iris.Context) {
	var maprule []model.MapRules

	err := ctx.ReadJSON(&maprule)

	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		ctx.JSON(service.AddRule(maprule))
	}

}

func UpdateRules(ctx iris.Context) {

}
